<template>
  <div v-loading.lock="loading"
    class="base-content">
    <div class="basic-info">
      <el-form ref="queryForm"
        :inline="true"
        :model="queryForm">
        <el-form-item label="检验单编号"
          prop="formNum">
          <el-input v-model="queryForm.formNum"
            placeholder="检验单号" />
        </el-form-item>
        <el-form-item label="来源单号"
          prop="orderNum">
          <el-input v-model="queryForm.orderNum"
            placeholder="来源单号" />
        </el-form-item>
        <el-form-item label="状态"
          prop="status">
          <el-select v-model="queryForm.status"
            placeholder="请选择">
            <el-option v-for="item in statusArray"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary"
            @click="onQuery">查询</el-button>
          <el-button @click="cancelForm">清空</el-button>
        </el-form-item>
      </el-form>

      <div class="base-content-operate">
        <el-button type="primary"
          @click="Export">导出</el-button>
      </div>
      <el-table ref="registTable"
        :data="list"
        highlight-current-row>
        <el-table-column type="selection"
          width="55"> </el-table-column>
        <el-table-column label="检验单号">
          <template slot-scope="scope">
            <el-link type="primary"
              @click="edit(scope.row)">
              {{ scope.row.formNum }}</el-link>
          </template>
        </el-table-column>
        <el-table-column label="来源单号">
          <template slot-scope="scope">
            {{ scope.row.orderNum }}
          </template>
        </el-table-column>

        <el-table-column label="检验单类型">
          <template slot-scope="scope">
            {{ scope.row.formTypeName }}
          </template>
        </el-table-column>
        <el-table-column label="仓库名称">
          <template slot-scope="scope">
            {{ scope.row.storageName }}
          </template>
        </el-table-column>
        <el-table-column label="检验员">
          <template slot-scope="scope">
            {{ scope.row.inspectorName }}
          </template>
        </el-table-column>
        <el-table-column label="检验日期">
          <template slot-scope="scope">
            {{ scope.row.inspectionDate | dateTimeFormat }}
          </template>
        </el-table-column>
        <el-table-column label="状态名称">
          <template slot-scope="scope">
            {{ scope.row.statusName }}
          </template>
        </el-table-column>
        <el-table-column label="创建人时间">
          <template slot-scope="scope">
            {{ scope.row.createTime }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <div class="base-table-operation">
              <el-link v-if="scope.row.status == 100 && scope.row.formTypeCode == 100"
                size="mini"
                type="danger"
                @click="Repeal(scope.row)">作废</el-link>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <BasePagination :total="pagination.total"
        :current-page="pagination.pageIndex"
        :page-count="pagination.total"
        @changePage="changePage"
        @sizeChange="sizeChange" />
    </div>

    <MaterialInspectionFormEdit v-if="dialogFormVisible"
      ref="MaterialInspectionFormEdit"
      :dialog-form-visible.sync="dialogFormVisible"
      :edit-id="editId" />
  </div>
</template>

<script>
import { getList, repeal, ExportWord, DownloadExportWordZip } from "../../../api/Material/MaterialInspectionForm.js";
import MaterialInspectionFormEdit from "./MaterialInspectionFormEdit.vue";
import { export_json_to_excel } from "@/vendor/Export2Excel"
import moment from 'moment';
export default {
  name: "MaterialInspectionFormList",
  components: { MaterialInspectionFormEdit },
  filters: {
    dateTimeFormat(dateTime) {
      if (!dateTime) {
        return dateTime;
      }
      return moment(dateTime).format("YYYY-MM-DD");
    },
  },
  data() {
    return {
      // 态(100.待检测、200检测中、300已检测、400已作废)"
      loading: true,
      dialogFormVisible: false,
      multipleSelection: [],
      tHeader: [
        "检验单编号",
        "来源单号",
        "检查类型",
        "仓库",
        "检验员",
        "检验日期",
        "状态",
        "创建时间",

      ],
      filterVal: [
        "formNum",
        "orderNum",
        "formTypeName",
        "storageName",
        "inspectorName",
        "inspectionDate",
        "statusName",
        "createTime"],
      formTypeCodeArray: [
        { value: 100, label: "接收检查" },
        { value: 200, label: "使用验证" }],
      statusArray: [
        { value: -1, label: "全部" },
        { value: 100, label: "已检测" },
        { value: 200, label: "已作废" }
      ],
      list: [],
      queryForm: {
        formNum: "",
        orderNum: "",
        status: -1,
      },
      pagination: {
        pageIndex: 1,
        pageSize: 10,
        total: 0,
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    export_json_to_excel,
    edit(row) {
      this.editId = row.id;
      this.dialogFormVisible = true;
    },

    formatJson(filterVal, jsonData) {
      return jsonData.map((v) =>
        filterVal.map((j) => {
          return v[j];
        })
      );
    },
    Repeal(row) {
      var obj = {};
      obj.id = row.id;
      obj.status = 200
      obj.statusName = "已作废"
      repeal(obj).then(res => {
        this.getList();
      })
    },
    Export() {
      var rows = this.$refs.registTable.selection;
      var ids = [];
      rows.map((item) => {
        ids.push(item.id)
      })
      if (ids?.length <= 0) {
        this.$message.error("请勾选需要导出的检验单");
        return false
      }

      ExportWord(ids).then(res => {
        DownloadExportWordZip({ path: res.data }).then((res) => {
          this.uploadLoad = false;
          var blob = new Blob([res.data], {
            type: "application/octet-stream",
          });
          var url = window.URL.createObjectURL(blob);
          const link = document.createElement("a"); // 创建a标签
          link.href = url;
          link.download = "检验单.zip"; // 重命名文件
          link.click();
          URL.revokeObjectURL(url); // 释放内存
        }).catch(() => {
          this.uploadLoad = false;
        });
      }).catch(res => { });
    },
    changePage(val) {
      this.pagination.pageIndex = val;
      this.getList();
    },
    sizeChange(val) {
      this.pagination.pageIndex = 1;
      this.pagination.pageSize = val;
      this.getList();
    },
    cancelForm() {
      this.$refs.queryForm.resetFields();
      this.pagination.pageIndex = 1;
      this.getList();
    },
    getList() {
      this.loading = true;
      getList(this.queryForm, this.pagination)
        .then((res) => {
          this.list = res.data.dataList ? res.data.dataList : [];
          this.pagination.total = res.data.total;
          this.loading = false;
        })
        .catch(() => {
          this.loading = false;
        });
    },

    onQuery() {
      this.getList();
    },
  },
};
</script>
